const app = getApp();
var util = require('../../utils/util.js');
var api = require('../../utils/api.js');
var user = require('../../utils/user.js');
Page({

  data: {
    showLoginModel: false,
    shoucang: false,
    distance: '100m'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // options.sycs = Math.round(Math.random() * 100);
    this.setData(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var t = this;
    wx.showLoading({
      title: '加载中...',
    });
    user.request(api.servicePhoneDetail, {
        token: app.globalData.token,
        id: t.data.id
      },
      function(data) {
        console.log(data.data);
        // 设置标注点
        var marker = [{
          id: data.data.id,
          latitude: data.data.lat,
          longitude: data.data.lng
        }];
        data.data.markers = marker;
        data.data.shoucang = data.shoucang;
        t.setData(data.data);
        // 设置标题
        wx.setNavigationBarTitle({
          title: data.data.name
        });
        // 计算距离
        app.getLocation(function(location) {
          var lat1 = location.lat;
          var lng1 = location.lng;
          var lat2 = data.data.lat;
          var lng2 = data.data.lng;
          var distance = util.getDistanceStr(lat1, lng1, lat2, lng2);
          console.log(distance);
          t.setData({
            distance: distance
          });
        });
        wx.hideLoading();
      });
  },
  revise: function(){
    var t = this
    wx.navigateTo({
      url: '../error/error?phoneId=' + t.data.id,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: this.data.name
    }
  },
  openTelephone: function() {
    var phone = this.data.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
    // if (app.globalData.haveUserInfo) {
    //   var phone = this.data.phone
    //   wx.makePhoneCall({
    //     phoneNumber: phone
    //   })
    // } else {
    //   this.setData({
    //     showLoginModel: true
    //   })
    // }
  },
  openLocation: function() {
    wx.openLocation({
      latitude: this.data.lat,
      longitude: this.data.lng,
      scale: 18,
      name: this.data.name,
      address: this.data.address,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  collection: function() {
    var t = this;
    var shoucang = t.data.shoucang;
    var phoneId = t.data.id;
    if (shoucang) {
      user.request(api.disCollection, {
        token: app.globalData.token,
        phoneId: phoneId
      }, function(data) {
        t.setData({
          shoucang: false
        })
      })
    } else {
      user.request(api.collection, {
        token: app.globalData.token,
        phoneId: phoneId
      }, function(data) {
        t.setData({
          shoucang: true
        })
      })
    }
  }
})